//library DB
function filesSelected(myFiles) {
  for (var i = 0, f; f = myFiles[i]; i++) {
    var videoReader = new FileReader();
    videoReader.onload = (function(aFile) {
      return function(e) {
    	  var typeStr = aFile.type;
    	  var nameStr = aFile.name;
    	  //alert( nameStr.substring(0,nameStr.lastIndexOf(".")) );
    	  if(typeStr.substring(0,5) == 'video'){
    	  	insertNote(e.target.result, nameStr.substring(0,nameStr.lastIndexOf(".")), typeStr.substring(6).toUpperCase(), aFile.size);
    	  }
      };
    })(f);
    videoReader.readAsDataURL(f);
  }
}


function insertNote(url, name, type, size)
{
	var db = openDatabase("html5playerDB", "1.0", "HTML5 Database API", 200000);
    db.transaction(function(tx) {
    	tx.executeSql("INSERT INTO video (url, name, type, size) VALUES (?,?,?,?)", [url, name, type, size], function(result){
    		
    		tx.executeSql("SELECT seq FROM sqlite_sequence where name = 'video' ", [], function(tx, result) {

    			var row = result.rows.item(0);
    			var _id = row['seq'];
    			
    			var list = document.getElementById('iframeId').contentDocument.getElementById('list');
    			var i = list.childNodes.length;

                var tr = document.createElement('tr');
                if(i%2 == 0) tr.setAttribute('class', 'even');
                else tr.setAttribute('class', 'odd');
                		
                tr.setAttribute('id', 'id'+_id)
                tr.innerHTML = ['<td id="t1">&nbsp;<input type="checkbox" /></td>',
                            	'<td id="t2"> ', i+1,  ' </td>', 
                            	'<td id="t3">&nbsp;&nbsp; ', name, ' </td>', 
                            	'<td id="t4"> ', type, ' </td>',
                            	'<td id="t5"> ', size, ' </td>', 
                                '<td id="t6"><input type="button" value=" X " onClick="delNote(', _id,')" /></td>',
                                '<td id="t7"><input type="button" value=" > " onClick="addList(', _id,')" /></td>'].join('');
                
                
                list.insertBefore(tr, null);
        	});
    		
    	});
    	
    });
}
//DB END



//playlist DB
function plistFileSelect(myFiles) {
  for (var i = 0, f; f = myFiles[i]; i++) {
    var videoReader = new FileReader();
    videoReader.onload = (function(aFile) {
      return function(e) {
    	  var typeStr = aFile.type;
    	  var nameStr = aFile.name;
    	  //alert( nameStr.substring(0,nameStr.lastIndexOf(".")) );
    	  if(typeStr.substring(0,5) == 'video'){
    		  insertPlaylist(e.target.result, nameStr.substring(0,nameStr.lastIndexOf(".")), typeStr.substring(6).toUpperCase(), aFile.size);
    	  }
      };
    })(f);
    videoReader.readAsDataURL(f);
  }
}


function insertPlaylist(url, name, type, size)
{
	var db = openDatabase("html5playerDB", "1.0", "HTML5 Database API", 200000);
    db.transaction(function(tx) {
    	tx.executeSql("INSERT INTO video_playlist (url, name, type, size) VALUES (?,?,?,?)", [url, name, type, size], function(result){
    		
    		tx.executeSql("SELECT seq FROM sqlite_sequence where name = 'video_playlist' ", [], function(tx, result) {

    			var row = result.rows.item(0);
    			var _id = row['seq'];
    			
    			var list = document.getElementById('iframe_play_list').contentDocument.getElementById('playlist');
    			var i = list.childNodes.length;
    	     
                var tr = document.createElement('tr');
                if(i%2 == 0) tr.setAttribute('class', 'even');
                else tr.setAttribute('class', 'odd');
                		
                tr.setAttribute('id', 'id'+_id)
                tr.innerHTML = ['<td id="pl_t1">', i+1, '</td>', 
                            	'<td id="pl_t2" onclick="play(', _id, ')">', name, '</td>', 
                                '<td id="pl_t3"><input type="button" value=" X " onClick="delNote(', _id,')" /></td>'].join('');
                
                
                list.insertBefore(tr, null);
        	});
    		
    	});
    	
    });
}
//DB END
